﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Meqs.Entity;
using Meqs.Entity.Views;
using Meqs.Logic.Interface;
using Meqs.Logic;

public partial class Base_School_StaffGrid : ContainerBasePage
{
    protected static int CauseID
    {
        get
        {
            int _causeID;
            if (Int32.TryParse(HttpContext.Current.Request["id"], out _causeID))
                return _causeID;
            return -1;
        }
    }

    protected static String EType
    {
        get
        {
            if (HttpContext.Current.Request["type"] == null)
                return "";
            return HttpContext.Current.Request["type"];
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void btnBind_Click(object sender, EventArgs e)
    {
        Bind(1);
    }

    private void Bind(int iPageIndex)
    {
        List<TeacherRoleInfoView> list = null;
        switch (EType)
        {
            case "Staff":
                list = (List<TeacherRoleInfoView>)Container<ITeacherRolesLogic>.Instance.FindTeacherInStaff(CauseID, SchoolMark, SchoolCommon.Sch_SchoolInfo.SchoolYear, SchoolCommon.Sch_SchoolInfo.SchoolTerm);
                break;
            case "Grade":
                list = (List<TeacherRoleInfoView>)Container<ITeacherRolesLogic>.Instance.FindTeacherInGrade(CauseID, SchoolMark, SchoolCommon.Sch_SchoolInfo.SchoolYear, SchoolCommon.Sch_SchoolInfo.SchoolTerm);
                break;
            case "Subject":
                list = (List<TeacherRoleInfoView>)Container<ITeacherRolesLogic>.Instance.FindTeacherInSubject(CauseID, SchoolMark, SchoolCommon.Sch_SchoolInfo.SchoolYear, SchoolCommon.Sch_SchoolInfo.SchoolTerm);
                break;
        }
        list = AnalyzeResult(list);
        ListSort<TeacherRoleInfoView> ls = new ListSort<TeacherRoleInfoView>(list, "TeachName");
        list = new List<TeacherRoleInfoView>(ls.Sort());
        //分页处理 
        PagerBind(list, iPageIndex);
    }

    private List<TeacherRoleInfoView> AnalyzeResult(List<TeacherRoleInfoView> list)
    {
        List<TeacherRoleInfoView> result = new List<TeacherRoleInfoView>();
        if (list == null || list.Count < 1)
            return result;

        Hashtable ht = new Hashtable();
        foreach (TeacherRoleInfoView view in list)
        {
            if (ht.ContainsKey(view.RID))
            {
                switch (EType)
                {
                    case "Staff":
                        break;
                    case "Grade":
                        if ((int)RoleKeyEnum.Teach_Director == view.RoleID && !(ht[view.RID] as TeacherRoleInfoView).DisplayString.Contains(view.RoleCaption))
                            (ht[view.RID] as TeacherRoleInfoView).DisplayString += (" " + view.RoleCaption + " ");
                        if ((int)RoleKeyEnum.School_Header == view.RoleID && !(ht[view.RID] as TeacherRoleInfoView).DisplayString.Contains(view.RoleCaption))
                            (ht[view.RID] as TeacherRoleInfoView).DisplayString += (" " + view.RoleCaption + " ");
                        if ((int)RoleKeyEnum.Subject_Director == view.RoleID && !(ht[view.RID] as TeacherRoleInfoView).DisplayString.Contains(view.RoleCaption))
                            (ht[view.RID] as TeacherRoleInfoView).DisplayString += (" " + view.RoleCaption + " ");
                        if ((int)RoleKeyEnum.GradeSubject_Director == view.RoleID && !(ht[view.RID] as TeacherRoleInfoView).DisplayString.Contains(view.RoleCaption))
                            (ht[view.RID] as TeacherRoleInfoView).DisplayString += (" " + view.RoleCaption + " ");
                        if ((int)RoleKeyEnum.Grade_Director == view.RoleID && !(ht[view.RID] as TeacherRoleInfoView).DisplayString.Contains(view.RoleCaption))
                            (ht[view.RID] as TeacherRoleInfoView).DisplayString += (" " + view.RoleCaption + " ");
                        if ((int)RoleKeyEnum.Class_Teacher == view.RoleID && !(ht[view.RID] as TeacherRoleInfoView).DisplayString.Contains(view.RoleCaption))
                            (ht[view.RID] as TeacherRoleInfoView).DisplayString += (" " + view.RoleCaption + " ");
                        if ((int)RoleKeyEnum.Class_Header == view.RoleID && !(ht[view.RID] as TeacherRoleInfoView).DisplayString.Contains(view.RoleCaption))
                            (ht[view.RID] as TeacherRoleInfoView).DisplayString += (" " + view.RoleCaption + " ");
                        break;
                    case "Subject":
                        if ((int)RoleKeyEnum.Teach_Director == view.RoleID && !(ht[view.RID] as TeacherRoleInfoView).DisplayString.Contains(view.RoleCaption))
                            (ht[view.RID] as TeacherRoleInfoView).DisplayString += (" " + view.RoleCaption + " ");
                        if ((int)RoleKeyEnum.School_Header == view.RoleID && !(ht[view.RID] as TeacherRoleInfoView).DisplayString.Contains(view.RoleCaption))
                            (ht[view.RID] as TeacherRoleInfoView).DisplayString += (" " + view.RoleCaption + " ");
                        if ((int)RoleKeyEnum.GradeSubject_Director == view.RoleID && !(ht[view.RID] as TeacherRoleInfoView).DisplayString.Contains(view.RoleCaption))
                            (ht[view.RID] as TeacherRoleInfoView).DisplayString += (" " + view.RoleCaption + " ");
                        if ((int)RoleKeyEnum.Grade_Director == view.RoleID && !(ht[view.RID] as TeacherRoleInfoView).DisplayString.Contains(view.RoleCaption))
                            (ht[view.RID] as TeacherRoleInfoView).DisplayString += (" " + view.RoleCaption + " ");
                        if ((int)RoleKeyEnum.Subject_Director == view.RoleID && !(ht[view.RID] as TeacherRoleInfoView).DisplayString.Contains(view.RoleCaption))
                            (ht[view.RID] as TeacherRoleInfoView).DisplayString += (" " + view.RoleCaption + " ");
                        if ((int)RoleKeyEnum.Class_Teacher == view.RoleID && !(ht[view.RID] as TeacherRoleInfoView).DisplayString.Contains(view.RoleCaption))
                            (ht[view.RID] as TeacherRoleInfoView).DisplayString += (" " + view.RoleCaption + " ");
                        if ((int)RoleKeyEnum.Class_Header == view.RoleID && !(ht[view.RID] as TeacherRoleInfoView).DisplayString.Contains(view.RoleCaption))
                            (ht[view.RID] as TeacherRoleInfoView).DisplayString += (" " + view.RoleCaption + " ");
                        break;
                }
            }
            else
            {
                view.DisplayString = (" " + view.RoleCaption + " ");
                ht.Add(view.RID, view);
            }
        }

        foreach (DictionaryEntry entry in ht)
        {
            result.Add(entry.Value as TeacherRoleInfoView);
        }

        return result;
    }

    //分页处理
    private void PagerBind(List<TeacherRoleInfoView> srcList, int pageIndex)
    {
        Repeater1.DataSource = null;
        Repeater1.DataBind();

        if (srcList == null || srcList.Count < 1)
        {
            return;
        }

        AspNetPager1.RecordCount = srcList.Count;
        AspNetPager1.CurrentPageIndex = pageIndex;

        int index = (pageIndex - 1) * AspNetPager1.PageSize;
        int count = 0;
        if (srcList.Count - index >= AspNetPager1.PageSize) count = AspNetPager1.PageSize;
        else count = srcList.Count - index;

        List<TeacherRoleInfoView> pageList = srcList.GetRange(index, count);

        Repeater1.DataSource = pageList;
        Repeater1.DataBind();
    }

    protected void AspNetPager1_PageChanged(object src, EventArgs e)
    {
        Wuqi.Webdiyer.PageChangedEventArgs pc = e as Wuqi.Webdiyer.PageChangedEventArgs;

        Bind(pc.NewPageIndex);
    }

    ///// <summary>
    ///// 移除教师的课程ID为iCauseID的任课信息
    ///// </summary>
    //[System.Web.Services.WebMethod]
    //public static int DeleteTeacher(int iCauseID, List<int> listRID)
    //{
    //    return Container<ITeachClsCauseLogic>.Instance.DeleteClsCauseTeacher(iCauseID, listRID);
    //}

    /// <summary>
    /// 提升为组长
    /// </summary>
    [System.Web.Services.WebMethod]
    public static int ToLeader(int teacherID, int causeID, int gradeID, String typec)
    {
        TeacherRoles tr = new TeacherRoles();
        tr.SchoolMark = SchoolMark;
        tr.SchoolYear = SchoolCommon.Sch_SchoolInfo.SchoolYear;
        tr.Term = SchoolCommon.Sch_SchoolInfo.SchoolTerm;
        tr.TeacherID = teacherID;
        tr.CauseID = causeID;
        tr.GreadID = gradeID;
        switch (typec)
        {
            case "Subject":
                tr.RoleID = (int)RoleKeyEnum.Subject_Director;
                break;
            case "Grade":
                tr.RoleID = (int)RoleKeyEnum.Grade_Director;
                break;
            //case "Staff":
            //    tr.RoleID = (int)RoleKeyEnum.GradeSubject_Director;
            //    break;
        }

        return Container<ITeacherRolesLogic>.Instance.Create(tr);
    }

    [System.Web.Services.WebMethod]
    public static int DownLeader(int teacherID, int causeID, int gradeID, String typec)
    {
        Hashtable ht = new Hashtable();
        ht.Add("TeacherID", teacherID);
       
        
        ht.Add("SchoolMark", SchoolMark);
        ht.Add("SchoolYear", SchoolCommon.Sch_SchoolInfo.SchoolYear);
        ht.Add("Term", SchoolCommon.Sch_SchoolInfo.SchoolTerm);
        switch (typec)
        {
            case "Subject":
                ht.Add("CauseID", causeID);
                ht.Add("RoleID", (int)RoleKeyEnum.Subject_Director);
                break;
            case "Grade":
                ht.Add("GreadID", gradeID);
                ht.Add("RoleID", (int)RoleKeyEnum.Grade_Director);
                break;
            //case "Staff":
            //    ht.Add("RoleID", (int)RoleKeyEnum.GradeSubject_Director);
            //    break;
        }

        return Container<ITeacherRolesLogic>.Instance.DeleteBy(ht);
    }

    /// <summary>
    /// 获取年级数据
    /// </summary>
    [System.Web.Services.WebMethod]
    public static IList<GradeGroup> GetTeacherGradeInfo(int teacherID, int causeID)
    {
        Hashtable ht = new Hashtable();
        ht.Add("TeacherID", teacherID);
        ht.Add("SchoolMark", SchoolMark);
        ht.Add("SchoolYear", SchoolCommon.Sch_SchoolInfo.SchoolYear);
        ht.Add("Term", SchoolCommon.Sch_SchoolInfo.SchoolTerm);
        ht.Add("CauseID", causeID);
        return Container<IGradeGroupLogic>.Instance.FindAll(ht);
    }

    /// <summary>
    /// 获取备课组长信息
    /// </summary>
    [System.Web.Services.WebMethod]
    public static IList<TeacherRoles> GetTeacherGSInfo(int teacherID, int causeID)
    {
        Hashtable ht = new Hashtable();
        ht.Add("TeacherID", teacherID);
        ht.Add("SchoolMark", SchoolMark);
        ht.Add("SchoolYear", SchoolCommon.Sch_SchoolInfo.SchoolYear);
        ht.Add("Term", SchoolCommon.Sch_SchoolInfo.SchoolTerm);
        ht.Add("CauseID", causeID);

        return Container<ITeacherRolesLogic>.Instance.FindAll(ht);
    }

    /// <summary>
    /// 更新备课组长信息
    /// </summary>
    [System.Web.Services.WebMethod]
    public static int UpdateGSLeader(int teacherID, int causeID, ArrayList checkedList, ArrayList uncheckedList)
    {
        foreach (int o in checkedList)
        {
            Hashtable ht = new Hashtable();
            ht.Add("TeacherID", teacherID);
            ht.Add("GreadID", o);
            ht.Add("CauseID", causeID);
            ht.Add("SchoolMark", SchoolMark);
            ht.Add("SchoolYear", SchoolCommon.Sch_SchoolInfo.SchoolYear);
            ht.Add("Term", SchoolCommon.Sch_SchoolInfo.SchoolTerm);
            ht.Add("RoleID", (int)RoleKeyEnum.GradeSubject_Director);
            if (Container<ITeacherRolesLogic>.Instance.FindCount(ht) > 0)
                continue;

            TeacherRoles tr = new TeacherRoles();
            tr.TeacherID = teacherID;
            tr.SchoolMark = SchoolMark;
            tr.SchoolYear = SchoolCommon.Sch_SchoolInfo.SchoolYear;
            tr.Term = SchoolCommon.Sch_SchoolInfo.SchoolTerm;
            tr.RoleID = (int)RoleKeyEnum.GradeSubject_Director;
            tr.GreadID = o;
            tr.CauseID = causeID;

            Container<ITeacherRolesLogic>.Instance.Create(tr);
        }
        foreach (int o in uncheckedList)
        {
            Hashtable ht = new Hashtable();
            ht.Add("TeacherID", teacherID);
            ht.Add("GreadID", o);
            ht.Add("CauseID", causeID);
            ht.Add("SchoolMark", SchoolMark);
            ht.Add("SchoolYear", SchoolCommon.Sch_SchoolInfo.SchoolYear);
            ht.Add("Term", SchoolCommon.Sch_SchoolInfo.SchoolTerm);
            ht.Add("RoleID", (int)RoleKeyEnum.GradeSubject_Director);
            Container<ITeacherRolesLogic>.Instance.DeleteBy(ht);
        }

        return 0;
    }

    [System.Web.Services.WebMethod]
    public static int RemoveSubject(int teacherID, int groupID)
    {
        Hashtable ht = new Hashtable();
        ht.Add("TeacherID", teacherID);
        ht.Add("CauseID", groupID);
        ht.Add("SchoolMark", SchoolMark);
        ht.Add("SchoolYear", SchoolCommon.Sch_SchoolInfo.SchoolYear);
        ht.Add("Term", SchoolCommon.Sch_SchoolInfo.SchoolTerm);
        Container<ITeacherRolesLogic>.Instance.DeleteBy(ht);

        List<int> listRID = new List<int>();
        listRID.Add(teacherID);
        return Container<ITeachClsCauseLogic>.Instance.DeleteClsCauseTeacher(groupID, listRID);
    }

    [System.Web.Services.WebMethod]
    public static int RemoveGrade(int teacherID, int groupID)
    {
        Hashtable ht = new Hashtable();
        ht.Add("TeacherID", teacherID);
        ht.Add("GreadID", groupID);
        ht.Add("SchoolMark", SchoolMark);
        ht.Add("SchoolYear", SchoolCommon.Sch_SchoolInfo.SchoolYear);
        ht.Add("Term", SchoolCommon.Sch_SchoolInfo.SchoolTerm);
        Container<ITeacherRolesLogic>.Instance.DeleteBy(ht);

        ht = new Hashtable();
        ht.Add("TeacherID", teacherID);
        ht.Add("Grade", groupID);
        ht.Add("SchoolMark", SchoolMark);
        ht.Add("SchoolYear", SchoolCommon.Sch_SchoolInfo.SchoolYear);
        ht.Add("Term", SchoolCommon.Sch_SchoolInfo.SchoolTerm);
        Container<IGradeGroupLogic>.Instance.DeleteBy(ht);
        return 0;
    }

    [System.Web.Services.WebMethod]
    public static int RemoveStaff(int teacherID, int groupID)
    {
        Hashtable ht = new Hashtable();
        ht.Add("TeacherID", teacherID);
        ht.Add("SchoolMark", SchoolMark);
        ht.Add("SchoolYear", SchoolCommon.Sch_SchoolInfo.SchoolYear);
        ht.Add("Term", SchoolCommon.Sch_SchoolInfo.SchoolTerm);
        ht.Add("RoleID", groupID);
        Container<ITeacherRolesLogic>.Instance.DeleteBy(ht);

        return 0;
    }
}
